// pages/common/signin/signin.js
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      isSignIn: false,
      title: "签到"
    },
    a: {
      "message": "签到成功",
      "user": "1",
    },
    b: {
      "category": "1",
      "origin": "签到",
      "user": "1",
      "value": 2
    },
    userInfo: {},
    integral: 0,
    status: false,
  },

  signin() {
    let form = this.data.form;
    let user = this.data.userInfo.id;
    let a = this.data.a;
    let b = this.data.b;
    if (form.isSignIn) {
      wx.showToast({
        title: '不可重复签到',
        icon: "none"
      })
    } else {
      a.user = user;
      b.user = user;
      wx.showLoading({
        title: '签到中',
      })
      wx.request({
        url: `${app.globalData.baseUrl}/integral`,
        complete: (res) => {
          wx.hideLoading();
        },
        data: b,
        fail: (res) => {
          wx.hideLoading();
          wx.showToast({
            title: '签到失败',
            icon: "none",
            duration: 4000
          });
        },
        method: "POST",
        success: (result) => {
          wx.request({
            url: `${app.globalData.baseUrl}/message/save`,
            data: a,
            method: "POST",
          });
          form.isSignIn = true;
          form.title = "已签到";
          this.setData({ form });
          wx.startPullDownRefresh();
        },
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onGetUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onGetUserInfo();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getStatus(user) {
    let _this = this;
    wx.request({
      url: `${app.globalData.baseUrl}/integral/states/${user}`,
      success: (result) => {
        let form = _this.data.form;
        console.log(form);
        console.log(result);
        if (result.data.dataMap.data.id) {
          form.isSignIn = true;
          form.title = "已签到";
          _this.setData({ form });
        } else {
          form.isSignIn = false;
          form.title = "签到";
          _this.setData({ form });
        }
      },
    })
  },
  getIntegral(user) {
    wx.request({
      url: `${app.globalData.baseUrl}/integral/sum/${user}`,
      complete: (res) => { },
      fail: (res) => { },
      success: (result) => {
        this.setData({ integral: result.data.dataMap.data });
      },
    })
  },
  onGetUserInfo() {
    let openid = wx.getStorageSync('openid');
    let _this = this;
    if (!openid) {
      wx.navigateBack({
        complete: (res) => {
          wx.showToast({
            title: '请登录',
            icon: "none",
          })
        }
      });
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: `${app.globalData.baseUrl}/user/selectIsUser/${openid}`,
        complete: (res) => { wx.hideLoading() },
        success: (result) => {
          _this.setData({ userInfo: result.data.dataMap.data });
          _this.getIntegral(result.data.dataMap.data.id)
          _this.getStatus(result.data.dataMap.data.id);
        },
      })
    }
  },
  onShareTimeline(){}
})